﻿/* 	Initialise the gadget. This function is called on 
	every loading process of the gadget*/
function init()
{

	if ( wave && wave.isInWaveContainer() )
	{
		wave.setStateCallback( stateUpdated );
	}

	_IG_AdjustIFrameHeight();								   			
	/* Set the page displayed according to the current play mode */
	
	buildTBDivs();
	setTimeout("firstInit()",400);
	setTimeout("ResultsToState()",400);
	setTimeout("RecommendationsToState()",2000);

	gadgets.window.adjustHeight();
}

gadgets.util.registerOnLoadHandler( init );

/* After the state has changed, refresh the display. */
function stateUpdated()
{	// all the functions below are used to synchronize the mutual display.
	// every function
	var game = gameState();
		alert(game);
	refreshGrid( game );	// 16X3 grid refreshed while playing and on transfer to results mode
	refreshAgreement(); 	// refresh the agreement traffic lights
	refreshAgreementDivs();	// refresh the agreement personal Divs (forms)
	refreshCost(); 			// refresh the cost
	RefreshGames();			// refresh the matches
	refreshTalkBacks(); 	// refresh the talkback divs
}

/* 	right after installing the gadget a NewForm() call has to be made.
	this function is called in every init() call, but it only affects 
	in the first call to init, hence the name.*/
function firstInit(){
	var IsPlaying = parseInt(wave.getState().get("IsPlaying"));
	if(!IsPlaying){
		curTime = currentTime();
		if(	(curTime.getDay() ==0) ||
			((curTime.getDay() ==1) && (curTime.getHours()< 16)) ||
			((curTime.getDay() ==6) && (curTime.getHours()> 11))) { 

			RefreshData(0);
			RefreshData(4);
		}
		else{
			NewForm(0);
		}
		getWaveIDfromDB();
	}
}

